﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AMM.Business.Store;
using AMM.CustomWinControls;
using AMM.Model;
using AMM.Model.Store;
using AMM.UserControls;

namespace AMM.Forms.Store {
    public partial class FrmVendor : FrmVendorBase {

        private int _pageIndex;
        private VendorSearchModel _searchModel;
        private VendorProcessor _vendorProcessor;
        private List<VendorViewModel> _vendorList;

        #region Override

        protected override string FunctionCode {
            get { return "AMM1401"; }
        }

        protected override BaseSearchControl SearchControl {
            get { return new VendorSearchControl(); }
        }

        protected override void Add() {
            base.Add();
            this.ResetForms();
            this.ShowEditForm("新增", new Vendor() { IsEnabled = true, ClassStatus = ClassStatus.Added });
        }

        protected override void Amend() {
            base.Amend();
            List<VendorViewModel> selectedList = this.dgvVendorList.GetSelectedRows<VendorViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            Vendor vendor = this._vendorProcessor.FetchVendorById(selectedList[0].Key);
            vendor.ClassStatus = ClassStatus.Updated;
            this.ShowEditForm("修改", vendor);
        }

        protected override void Save() {
            base.Save();
        }

        protected override void Delete() {
            base.Delete();
            List<VendorViewModel> selectedList = this.dgvVendorList.GetSelectedRows<VendorViewModel>();
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._vendorProcessor.Delete(selectedList.Select(x => x.Key).ToArray());
                this.MDIParent.SetStatusInformation("删除成功");
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

        protected override void Search(object sender, FrmSearch.SearchArgs e) {
            this.CreateNewThread(() => {
                if (e.IsRevert) {
                    this._searchModel = new VendorSearchModel();
                }
                else {
                    this._searchModel = (VendorSearchModel)e.SearchModel;
                }
            }, () => {
                this.BindVendorList();
            });
        }

        #endregion

        public FrmVendor() {
            InitializeComponent();

            this.dgvVendorList.DataGridViewModelType = typeof(VendorViewModel);
        }

        #region Private methods

        private bool frmEdit_Edit(object sender, FrmEdit.EditArgs e) {
            bool resultFlag = false;
            this.PreventCancel = true;
            Vendor vendor = (Vendor)e.EditModel;
            this.CreateNewThread(() => {
                if (vendor.ClassStatus == ClassStatus.Added) {
                    vendor.CreatedBy = LoginUserName;
                    this._vendorProcessor.Insert(vendor);
                }
                else {
                    vendor.ModifiedBy = LoginUserName;
                    this._vendorProcessor.Update(vendor);
                }
                resultFlag = true;
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                if (resultFlag) {
                    this.MDIParent.SetStatusInformation("保存成功");
                    this.FormEdit.Close();
                }
            });
            this.Save();
            return true;
        }

        private void ResetForms() {
            this.BindVendorList();
        }

        private void BindVendorList() {
            this.CreateNewThread(() => {
                this._vendorList = this._vendorProcessor.FetchVendorViewModelList(this._searchModel, this._pageIndex);
            }, () => {
                this.dgvVendorList.DataSource = new SortableBindingList<VendorViewModel>(this._vendorList);
                this.pceList.Records = this._searchModel.Records;
                this.pceList.DataBind();
            });
        }

        private void ShowEditForm(string title, Vendor vendor) {
            VendorEditControl editControl = new VendorEditControl();
            this.FormEdit = new FrmEdit();
            editControl.BaseEditModel = vendor;
            editControl.ValidatorForEdit = this.FormEdit.validatorForEdit;
            this.FormEdit.Controls.Add(editControl);
            this.FormEdit.Edit += new FrmEdit.EditEventHandler(frmEdit_Edit);
            this.FormEdit.Text = string.Format("{0}{1}", title, this.Text);
            FrmEntry.LoadAndRunDialog(this.FormEdit);
        }

        #endregion

        private void FrmVendor_Load(object sender, EventArgs e) {
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._vendorProcessor = new VendorProcessor(this.pceList.PageSize);
                this._searchModel = new VendorSearchModel();
                this._pageIndex = 1;
            }, () => {
                this.WindowState = FormWindowState.Maximized;
                this.BindVendorList();
                this.PreventCancel = false;
            });
        }

        private void pceList_Paging(object sender, PaginationArgs e) {
            this._pageIndex = e.PageIndex;
            this.BindVendorList();
        }

    }
}
